//
//  HomeViewModel.m
//  MiWei
//
//  Created by 刘星 on 2017/10/13.
//  Copyright © 2017年 miwei. All rights reserved.
//

#import "HomeViewModel.h"
#import "ReactiveCocoa.h"
#import "HTTPTool.h"
#import "MJExtension.h"
#import "HomeTableView.h"
#import "MJRefresh.h"
#import "HomeListModel.h"
#import "HomeTableHeadView.h"
#import "HomeRecomendModel.h"
#import "HomeAdModel.h"

@interface HomeViewModel ()

@property (nonatomic, assign) NSInteger page;

@end

@implementation HomeViewModel

// 初始化
- (instancetype)initWithService:(id<ViewModelServices>)service params:(NSDictionary *)params {
    self = [super initWithService:service params:params];
    if (self) {
        self.page = 1;
        [self initViewModel];
    }
    return self;
}

- (void)initViewModel {
    @weakify(self);
    // 热门推荐
    self.loadDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        self.page = 1;
        NSString *recomendUrl = @"http://www.91miwei.com/app/index/recommend?page.currentPage=1";
        RACSignal *recomendSignal = [HTTPTool postWithURL:recomendUrl  withParamater:nil];
        [recomendSignal subscribeNext:^(id x) {
            @strongify(self);
//            NSLog(@"%@", x);
            [self.recommendDataArray removeAllObjects];
            if ([x[@"success"] integerValue] != 0) {
                [self.recommendDataArray addObjectsFromArray:[HomeListModel mj_objectArrayWithKeyValuesArray:x[@"entity"][@"appRecommendList"]]];
            }
            HomeTableView *tableView = input;
            if (tableView.mj_header.isRefreshing) {
                [tableView.mj_header endRefreshing];
            }
            [tableView reloadData];
        }];
        return recomendSignal;
    }];
    
    self.loadRecommendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        NSString *recommendUrlStr = @"http://www.91miwei.com/app/index/courseSort/2";
        RACSignal *adSignal = [HTTPTool postWithURL:recommendUrlStr withParamater:nil];
        [adSignal subscribeNext:^(id x) {
            @strongify(self);
            NSLog(@"%@", x);
            // 本周推荐
            [self.weekRecommendDataArray removeAllObjects];
            [self.weekRecommendDataArray addObjectsFromArray:[HomeRecomendModel mj_objectArrayWithKeyValuesArray:x[@"entity"]]];
            HomeTableHeadView *headView = input;
            if (headView.tableView.mj_header.isRefreshing) {
                [headView.tableView.mj_header endRefreshing];
            }
            [headView reloadData];
        }];
        return adSignal;
    }];
    
    // 刷新头部
    self.refreshHeadCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        RACSignal *adSignal = [HTTPTool postWithURL:@"http://www.91miwei.com/app/index/banner" withParamater:nil];
        [adSignal subscribeNext:^(id x) {
            @strongify(self);
            // 广告页面
            [self.adArray removeAllObjects];
            [self.adModelArray removeAllObjects];
            NSMutableArray *array = [NSMutableArray array];
            [array addObjectsFromArray:[HomeAdModel mj_objectArrayWithKeyValuesArray:x[@"entity"][@"indexCenterBanner"]]];
            [self.adModelArray addObjectsFromArray:[HomeAdModel mj_objectArrayWithKeyValuesArray:x[@"entity"][@"indexCenterBanner"]]];
            for (HomeAdModel *model in array) {
                NSString *url = [NSString stringWithFormat:@"%@%@", @"http://static.91miwei.com", model.imagesUrl];
                [self.adArray addObject:url];
            }
            HomeTableHeadView *headView = input;
            [headView reloadData];
        }];
        return adSignal;
    }];
}

- (NSMutableArray *)recommendDataArray {
    if (_recommendDataArray == nil) {
        _recommendDataArray = [NSMutableArray array];
    }
    return _recommendDataArray;
}

- (NSMutableArray *)weekRecommendDataArray {
    if (_weekRecommendDataArray == nil) {
        _weekRecommendDataArray = [NSMutableArray array];
    }
    return _weekRecommendDataArray;
}

- (NSMutableArray *)adArray {
    if (_adArray == nil) {
        _adArray = [NSMutableArray array];
    }
    return _adArray;
}

- (NSMutableArray *)adModelArray {
    if (_adModelArray == nil) {
        _adModelArray = [NSMutableArray array];
    }
    return _adModelArray;
}

@end
